package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.ExportWorkStaffVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.service.staff.StaffExcelNoFormService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.weblist.pojo.dto.WorkReportStaticExportDTO;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author mingliang
 * @Description 非表单的导出
 * @Date 2022/4/8 14:48
 **/

@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/file/excel/noForm")
public class StaffExcelNoFormController {

    @Resource
    private StaffExcelNoFormService staffExcelNoFormService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 导出工作日报
     */
    @RequestMapping(value = "/export/dailyWork",method = RequestMethod.POST)
    public String exportDailyWork(@RequestBody @Valid WorkReportStaticExportDTO workReportStaticExportDTO, BindingResult br, HttpServletRequest request, HttpServletResponse httpServletResponse) throws Exception {
        XbbResponse<ExportWorkStaffVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportWorkStaffVO exportStaffVO = staffExcelNoFormService.exportDailyWork(workReportStaticExportDTO);
                response = new XbbResponse<>(exportStaffVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
                String hashKey = workReportStaticExportDTO.getCorpid() + "_" + workReportStaticExportDTO.getUserId();
                paasRedisHelper.removeValue(RedisPrefixConstant.EXPORT_STATUS_KEY, hashKey);
            } catch (Exception unHandledException) {
                response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100001, unHandledException.getMessage()));
                String hashKey = workReportStaticExportDTO.getCorpid() + "_" + workReportStaticExportDTO.getUserId();
                paasRedisHelper.removeValue(RedisPrefixConstant.EXPORT_STATUS_KEY, hashKey);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }


}
